package com.qiangshi.taskClaim.controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.qiangshi.common.core.domain.entity.SysUser;
import com.qiangshi.framework.web.service.TokenService;
import com.qiangshi.system.service.ISysUserService;
import com.qiangshi.tasks.domain.QsProjectTasks;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qiangshi.common.annotation.Log;
import com.qiangshi.common.core.controller.BaseController;
import com.qiangshi.common.core.domain.AjaxResult;
import com.qiangshi.common.enums.BusinessType;
import com.qiangshi.taskClaim.domain.QsTaskClaim;
import com.qiangshi.taskClaim.service.IQsTaskClaimService;
import com.qiangshi.common.utils.poi.ExcelUtil;
import com.qiangshi.common.core.page.TableDataInfo;

import static java.util.stream.Collectors.toList;

/**
 * 认领记录Controller
 *
 * @author qiangshi
 * @date 2022-02-12
 */
@RestController
@RequestMapping("/taskClaim/taskClaim")
public class QsTaskClaimController extends BaseController
{
    @Autowired
    private IQsTaskClaimService qsTaskClaimService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询认领记录列表
     */
    @PreAuthorize("@ss.hasPermi('taskClaim:taskClaim:list')")
    @GetMapping("/list")
    public TableDataInfo list(QsTaskClaim qsTaskClaim)
    {
        startPage();
        List<QsTaskClaim> list = qsTaskClaimService.selectQsTaskClaimList(qsTaskClaim);
        List<SysUser> sysUserList = sysUserService.selectUserList(new SysUser());
        Map<Long,SysUser> sysUserMap = sysUserList.stream().collect(Collectors.groupingBy(SysUser::getUserId,Collectors.collectingAndThen(toList(), value->value.get(0))));
        for (QsTaskClaim projectTasks : list) {
            if(sysUserMap.containsKey(projectTasks.getPublisherId())){
                projectTasks.setPublisherName(sysUserMap.get(projectTasks.getPublisherId()).getUserName());
            }
            if(sysUserMap.containsKey(projectTasks.getClaimerId())){
                projectTasks.setClaimerName(sysUserMap.get(projectTasks.getClaimerId()).getUserName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出认领记录列表
     */
    @PreAuthorize("@ss.hasPermi('taskClaim:taskClaim:export')")
    @Log(title = "认领记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(QsTaskClaim qsTaskClaim)
    {
        List<QsTaskClaim> list = qsTaskClaimService.selectQsTaskClaimList(qsTaskClaim);
        ExcelUtil<QsTaskClaim> util = new ExcelUtil<QsTaskClaim>(QsTaskClaim.class);
        return util.exportExcel(list, "taskClaim");
    }

    /**
     * 获取认领记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('taskClaim:taskClaim:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(qsTaskClaimService.selectQsTaskClaimById(id));
    }

    /**
     * 新增认领记录
     */
    @PreAuthorize("@ss.hasPermi('taskClaim:taskClaim:add')")
    @Log(title = "认领记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QsTaskClaim qsTaskClaim)
    {
        return toAjax(qsTaskClaimService.insertQsTaskClaim(qsTaskClaim));
    }

    /**
     * 修改认领记录
     */
    @PreAuthorize("@ss.hasPermi('taskClaim:taskClaim:edit')")
    @Log(title = "认领记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QsTaskClaim qsTaskClaim)
    {
        return toAjax(qsTaskClaimService.updateQsTaskClaim(qsTaskClaim));
    }

    /**
     * 删除认领记录
     */
    @PreAuthorize("@ss.hasPermi('taskClaim:taskClaim:remove')")
    @Log(title = "认领记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(qsTaskClaimService.deleteQsTaskClaimByIds(ids));
    }
}
